package com.xhwl.logistics.converter.mapstruct;

import com.google.common.base.Strings;
import com.xhwl.logistics.bo.input.QueryVehicleAccessRecordThirdBO;
import com.xhwl.logistics.bo.input.QueryVehicleOvernightConditionBO;
import com.xhwl.logistics.component.VehicleConfigProperty;
import com.xhwl.logistics.component.VehicleHomeAddressProperty;
import com.xhwl.logistics.constant.VehicleConstant;
import com.xhwl.logistics.dto.query.VehicleAccessRecordExcelDTO;
import com.xhwl.logistics.entity.excel.VehicleAccessRecordExcel;
import com.xhwl.logistics.enums.VehicleCardTypeEnum;
import com.xhwl.logistics.enums.VehiclePassTypeEnum;
import com.xhwl.logistics.enums.VehicleQuerySortEnum;
import com.xhwl.logistics.po.VehicleAccessRecordSecondPO;
import com.xhwl.logistics.util.AboutDateUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.NullValueMappingStrategy;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Mapper(componentModel = "spring", nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT, uses = {VehicleCommonConverter.class})
public abstract class VehicleExcelConverter {

    @Autowired
    private VehicleHomeAddressProperty vehicleHomeAddressProperty;

    @Autowired
    private VehicleConfigProperty vehicleConfigProperty;

    //************Query BO <- Controller DTO************
    @Mapping(target = "parkId", ignore = true)
    @Mapping(target = "sortDirection", ignore = true)
    @Mapping(target = "queryVehicleOvernightConditionBO", source = "overnight")
    public abstract QueryVehicleAccessRecordThirdBO vehicleAccessRecordExcelDTO2QueryVehicleAccessRecordThirdBO(VehicleAccessRecordExcelDTO vehicleAccessRecordExcelDTO);

    public QueryVehicleAccessRecordThirdBO toQueryVehicleAccessRecordThirdBO(VehicleAccessRecordExcelDTO vehicleAccessRecordExcelDTO, String parkId) {
        QueryVehicleAccessRecordThirdBO queryVehicleAccessRecordThirdBO = vehicleAccessRecordExcelDTO2QueryVehicleAccessRecordThirdBO(vehicleAccessRecordExcelDTO);
        queryVehicleAccessRecordThirdBO.setParkId(parkId);

        String plateNumber = queryVehicleAccessRecordThirdBO.getPlateNumber();
        String passageName = queryVehicleAccessRecordThirdBO.getPassageName();
        Integer passType = queryVehicleAccessRecordThirdBO.getPassType();
        Date enterStartTime = queryVehicleAccessRecordThirdBO.getEnterStartTime();
        Date enterEndTime = queryVehicleAccessRecordThirdBO.getEnterEndTime();
        QueryVehicleOvernightConditionBO queryVehicleOvernightConditionBO = queryVehicleAccessRecordThirdBO.getQueryVehicleOvernightConditionBO();
        if (StringUtils.isAllEmpty(plateNumber, passageName) && ObjectUtils.allNull(passType, enterStartTime, enterEndTime, queryVehicleOvernightConditionBO)) {
            queryVehicleAccessRecordThirdBO.setMultiSheet(true);

            final int intervalMonth = 5;
            Date now = new Date();
            Date start = AboutDateUtil.dateMinusMonth(AboutDateUtil.theSameMonthOfDate(now, VehicleConstant.vehicleTimeZone), intervalMonth);
            queryVehicleAccessRecordThirdBO.setEnterStartTime(start);
            queryVehicleAccessRecordThirdBO.setEnterEndTime(now);
        } else {
            queryVehicleAccessRecordThirdBO.setMultiSheet(false);
        }

        String sortName = vehicleAccessRecordExcelDTO.getSortName();
        if (Strings.isNullOrEmpty(sortName)) {
            queryVehicleAccessRecordThirdBO.setSortColumn(VehicleQuerySortEnum.ACCESS_RECORD_SORT_ID.getSortColumn());
        } else {
            queryVehicleAccessRecordThirdBO.setSortColumn(VehicleQuerySortEnum.toSortColumn(sortName));
        }
        String sortDirection = vehicleAccessRecordExcelDTO.getSortDirection();
        if (Strings.isNullOrEmpty(sortDirection)) {
            queryVehicleAccessRecordThirdBO.setSortDirection(VehicleConstant.SORT_DIRECTION_DESC);
        } else {
            queryVehicleAccessRecordThirdBO.setSortDirection(VehicleQuerySortEnum.toDirection(sortDirection));
        }

        return queryVehicleAccessRecordThirdBO;
    }
    //************Query BO <- Controller DTO************


    //************Excel Entity <- PO************
    private String judgeOvernight(Date in, Date out) {
        if (null!=in && null!=out) {
            Date overnightDate = AboutDateUtil.overnightDate(in, vehicleConfigProperty.getZone(), vehicleConfigProperty.getDay(), vehicleConfigProperty.getHour(), vehicleConfigProperty.getMinute());
            return out.getTime() > overnightDate.getTime() ? "是" : "否";
        }
        return "/";
    }

    public abstract List<VehicleAccessRecordExcel> vehicleAccessRecordSecondPOList2VehicleAccessRecordExcelList(List<VehicleAccessRecordSecondPO> vehicleAccessRecordSecondPOList);
    public VehicleAccessRecordExcel vehicleAccessRecordSecondPO2VehicleAccessRecordExcel(VehicleAccessRecordSecondPO vehicleAccessRecordSecondPO) {
        VehicleAccessRecordExcel vehicleAccessRecordExcel = new VehicleAccessRecordExcel();

        String plateNumber = vehicleAccessRecordSecondPO.getPlateNumber();

        vehicleAccessRecordExcel.setProjectName(vehicleAccessRecordSecondPO.getProjectName());
        vehicleAccessRecordExcel.setPlateNumber(plateNumber);
        vehicleAccessRecordExcel.setEnterPassage(vehicleAccessRecordSecondPO.getEnterPassage());
        vehicleAccessRecordExcel.setExitPassage(vehicleAccessRecordSecondPO.getExitPassage());

        if (!Strings.isNullOrEmpty(plateNumber) && plateNumber.length() > 2) {
            vehicleAccessRecordExcel.setHomeAddress(vehicleHomeAddressProperty.getHomeAddress(plateNumber.substring(0, 2)));
        } else {
            vehicleAccessRecordExcel.setHomeAddress("/");
        }

        Integer cardType = vehicleAccessRecordSecondPO.getCardType();
        if (Objects.nonNull(cardType)) {
            vehicleAccessRecordExcel.setCardType(VehicleCardTypeEnum.toVehicleCardTypeEnum(cardType).getDescription());
        }

        Date enterTime = vehicleAccessRecordSecondPO.getEnterTime();
        if (Objects.nonNull(enterTime)) {
            vehicleAccessRecordExcel.setEnterTime(AboutDateUtil.dateToString(enterTime));
        }
        Integer enterType = vehicleAccessRecordSecondPO.getEnterType();
        if (Objects.nonNull(enterType)) {
            vehicleAccessRecordExcel.setEnterType(VehiclePassTypeEnum.toVehiclePassTypeEnum(enterType).getDescription());
        }

        Date exitTime = vehicleAccessRecordSecondPO.getExitTime();
        if (Objects.nonNull(exitTime)) {
            vehicleAccessRecordExcel.setExitTime(AboutDateUtil.dateToString(exitTime));
        }
        Integer exitType = vehicleAccessRecordSecondPO.getExitType();
        if (Objects.nonNull(exitType)) {
            vehicleAccessRecordExcel.setExitType(VehiclePassTypeEnum.toVehiclePassTypeEnum(exitType).getDescription());
        }

        vehicleAccessRecordExcel.setStayTime(AboutDateUtil.durationString(exitTime, enterTime));

        vehicleAccessRecordExcel.setOvernight(judgeOvernight(enterTime, exitTime));

        return vehicleAccessRecordExcel;
    }
    //************Excel Entity <- PO************

}
